package com.graduation.apiservice.controller;

import com.graduation.common.domain.dto.PageDTO;
import com.graduation.common.utils.UserContext;
import com.graduation.pojo.dto.FavoriteToggleDTO;
import com.graduation.pojo.query.FavoriteQuery;
import com.graduation.pojo.vo.FavoriteVO;
import com.graduation.service.FavoriteService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/favorite")
@RequiredArgsConstructor
public class FavoriteController {

    private final FavoriteService favoriteService;

    /**
     * 分页查询收藏列表
     */
    @GetMapping("/page")
    public PageDTO<FavoriteVO> pageFavorites(FavoriteQuery query) {
        Long currentUserId = UserContext.getUser();
        query.setUserId(currentUserId);
        return favoriteService.pageFavorites(query);
    }

    /**
     * 切换收藏状态（收藏/取消收藏）
     */
    @PostMapping("/toggle")
    public void toggleFavorite(@RequestBody FavoriteToggleDTO dto) {
        Long currentUserId = UserContext.getUser();
        dto.setUserId(currentUserId);
        favoriteService.toggleFavorite(dto);
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    public Boolean checkFavorite(
            @RequestParam(required = false) Long documentId,
            @RequestParam(required = false) Long caseId) {
        Long currentUserId = UserContext.getUser();
        return favoriteService.checkFavorite(currentUserId, documentId, caseId);
    }

}
